package com.fk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fk.reggie.common.R;
import com.fk.reggie.domain.Category;
import com.fk.reggie.domain.Dish;
import com.fk.reggie.domain.Setmeal;
import com.fk.reggie.domain.SetmealDish;
import com.fk.reggie.dto.DishDto;
import com.fk.reggie.dto.SetmealDto;
import com.fk.reggie.service.CategoryService;
import com.fk.reggie.service.DishService;
import com.fk.reggie.service.SetmealDishService;
import com.fk.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    //请求 URL: http://localhost:8080/setmeal
    //请求方法: POST

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//新增套餐 删除全部的套餐缓存 只有用户访问才添加缓存
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info(setmealDto.toString());
        setmealService.saveSetmealWithDish(setmealDto);
        return R.success("添加套餐成功");
    }

    //请求 URL: http://localhost:8080/setmeal/page?page=1&pageSize=10&name=111
    //请求方法: GET
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name), Setmeal::getName, name);
        setmealService.page(setmealPage, lambdaQueryWrapper);

        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");

        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtos = null;
        setmealDtos = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtos);
        return R.success(setmealDtoPage);
    }

    //请求 URL: http://localhost:8080/setmeal?ids=1415580119015145474
//请求方法: DELETE
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//删除套餐 删除全部的套餐缓存 只有用户访问才添加缓存
    public R<String> deleteWithDish(@RequestParam List<Long> ids) {
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }

    //请求 URL: http://localhost:8080/setmeal/status/0?ids=1415580119015145474,1636647735697014786
    //请求方法: POST
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)//更新套餐状态删除全部的套餐缓存 只有用户访问才添加缓存
    public R<String> updateStatus(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {
        setmealService.updateStatus(status, ids);
        return R.success("修改成功");
    }

    //请求 URL: http://localhost:8080/setmeal/1415580119015145474
    //请求方法: GET
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealById(@PathVariable("id") Long id) {
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDto.setSetmealDishes(setmealDishList);
        return R.success(setmealDto);
    }

    //请求 URL: http://localhost:8080/setmeal
    //请求方法: PUT
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐成功");
    }

    //请求 URL: http://localhost:8080/setmeal/list?categoryId=1636647430892748802&status=1
//请求方法: GET  用于前台请求  进行查询套餐下 指定全部套餐信息
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, 1)
                .eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId())
                .orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(setmealList);
    }

    //Request URL: http://localhost:8080/setmeal/dish/1415580119015145474
    //Request Method: GET
    //查看套餐信息

    /**
     * 移动端点击套餐图片查看套餐具体内容
     * 这里返回的是dto 对象，因为前端需要copies这个属性
     * 前端主要要展示的信息是:套餐中菜品的基本信息，图片，菜品描述，以及菜品的份数
     * @param SetmealId
     * @return
     */
    //这里前端是使用路径来传值的，要注意，不然你前端的请求都接收不到，就有点尴尬哈
    @GetMapping("/dish/{id}")
    public R<List<DishDto>> dish(@PathVariable("id") Long SetmealId){
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,SetmealId);
        //获取套餐里面的所有菜品  这个就是SetmealDish表里面的数据
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        List<DishDto> dishDtos = list.stream().map((setmealDish) -> {
            DishDto dishDto = new DishDto();
            //其实这个BeanUtils的拷贝是浅拷贝，这里要注意一下
            BeanUtils.copyProperties(setmealDish, dishDto);
            //这里是为了把套餐中的菜品的基本信息填充到dto中，比如菜品描述，菜品图片等菜品的基本信息
            Long dishId = Long.valueOf(setmealDish.getDishId());
            Dish dish = dishService.getById(dishId);
            BeanUtils.copyProperties(dish, dishDto);

            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtos);
    }
    //There is no getter for property named 'id'
    // in 'class com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper'
}
